﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdCreateGbmpLineFamilyInstance.h"
#include "GmUiStructureCommandRegister.h"
#include "StructureCmdIds.h"
#include "LineFamilyInstanceInput.h"
#include "StructureLinearInstanceCreator.h"
#include "GcmpBuiltInCategoryUniIdentities.h"
#include "StructureInstancePresetParameterUtils.h"
#include "IModelView.h"
#include "GbmpModelViewUtil.h"
#include "GbmpModelViewTypes.h"
#include "UiCommonDialog.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

REGISTER_GM_COMMAND(CmdCreateGbmpLineFamilyInstance)

CmdCreateGbmpLineFamilyInstance::CmdCreateGbmpLineFamilyInstance()
    : CmdCreateStraightStructureInstanceBase(ID_CMD_CREATE_GBMP_LINE_FAMILY_INSTANCE)
{
}

CmdCreateGbmpLineFamilyInstance::~CmdCreateGbmpLineFamilyInstance()
{
}

StructureFamilyUiUtils::FamilyCreators CmdCreateGbmpLineFamilyInstance::GetDefaultFamilyCreators()
{
    StructureFamilyUiUtils::FamilyCreators defaultFamilyCreators{}; // 为空
    return defaultFamilyCreators;
}

std::wstring CmdCreateGbmpLineFamilyInstance::GetDefaultFamilyName()
{
    return L"";
}

OwnerPtr<StructureLinearInstanceInput> CmdCreateGbmpLineFamilyInstance::GetInstanceInput(const std::vector<Vector3d>& pts) const
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    // 配置input数据
    OwnerPtr<LineFamilyInstanceInput> lineFamilyInput = NEW_AS_OWNER_PTR(LineFamilyInstanceInput);
    lineFamilyInput->Document = pDoc;
    lineFamilyInput->FamilyId = m_familyId;
    lineFamilyInput->InstanceTypeId = m_instanceTypeId;
    lineFamilyInput->PositionType = ElementInputPositionType::Line;
    lineFamilyInput->PositionPoints = pts;
    //工作平面
    lineFamilyInput->BaseId = GbmpModelViewUtil::GetReferencePlaneIdOfWorkPlane(pModelView);

    // 关联平面
    ElementId associatedPlaneId = GbmpModelViewUtil::GetWorkLevelId(pModelView);
    StructureInstancePresetParameterUtils::GetPresetBaseLevel(pDoc, GetCategory(), GetCategorySecondKey(), associatedPlaneId);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(associatedPlaneId.IsValid(), L"associatedPlaneId无效",L"GDMPLab",L"2024-03-30");
    lineFamilyInput->AssociatedPlaneId = associatedPlaneId;

    // 底部偏移
    double baseLevelOffset(0);
    StructureInstancePresetParameterUtils::GetPresetBaseLevelOffset(pDoc, GetCategory(), GetCategorySecondKey(), baseLevelOffset);
    lineFamilyInput->AssociatedPlaneOffset = baseLevelOffset;

    return TransferOwnership(lineFamilyInput);
}

OwnerPtr<IStructureLinearInstanceCreator> CmdCreateGbmpLineFamilyInstance::GetInstanceCreator() const
{
    return NEW_AS_OWNER_PTR(GbmpLineFamilyInstanceCreator);
}

Int32 CmdCreateGbmpLineFamilyInstance::GetCategorySecondKey() const
{
    return 0;
}

void CmdCreateGbmpLineFamilyInstance::SetupRibbonContextualPanel()
{
}

UniIdentity CmdCreateGbmpLineFamilyInstance::GetCategory() const
{
    UniIdentity categoryUid = BuiltInCategoryUniIdentities::BICU_LINE_FAMILY;
    return categoryUid;
}

bool CmdCreateGbmpLineFamilyInstance::OnExecuteCommand() const
{
    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    const std::wstring& viewType = pModelView->GetUserDataId();

    if (PlanViewType != viewType && View3DViewType != viewType)
    {
        UiCommonDialog::ShowMessageBox(GBMP_TR(L"绘制线式构件"), GBMP_TR(L"此视图下不支持绘制线式构件"), (int)UiCommonDialog::ButtonType::OK);
        return false;
    }
    return true;
}
